//
//  BHCompetitionAreaChoseView.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/8/19.
//

import UIKit
protocol BHCompetitionAreaChoseViewDelegate {
    func didChoseProvince(model: BHHomeCityModel)
    func didChoseCity(model: BHHomeCityModel)
    func didChoseArea(areaModel: BHHomeCityModel)
    func didPlaceChoseClose()
}

class BHCompetitionAreaChoseView: UIView {
    
    var delegate: BHCompetitionAreaChoseViewDelegate?
    
    
    lazy var locationSecView: BHCompetitionAreaLocationView = {
        let view = BHCompetitionAreaLocationView.loadFromXib()
        return view
    }()
    
    lazy var provinceTab: UITableView = {
        let view = UITableView.buildTableView(style: .plain)
        view.register(UINib.init(nibName: "BHHomeChoseCityCell", bundle: nil), forCellReuseIdentifier: "BHHomeChoseCityCell")
        view.delegate = self
        view.dataSource = self
        view.bounces = false
        return view
    }()
    
    lazy var cityTab: UITableView = {
        let view = UITableView.buildTableView(style: .plain)
        view.register(UINib.init(nibName: "BHHomeChoseCityCell", bundle: nil), forCellReuseIdentifier: "BHHomeChoseCityCell")
        view.delegate = self
        view.dataSource = self
        view.backgroundColor = .color_f8f8f8
        view.bounces = false
        return view
    }()
    
    // 赛事的筛选专属
    lazy var areaTab: UITableView = {
        let view = UITableView.buildTableView(style: .plain)
        view.register(UINib.init(nibName: "BHHomeChoseCityCell", bundle: nil), forCellReuseIdentifier: "BHHomeChoseCityCell")
        view.delegate = self
        view.dataSource = self
        view.backgroundColor = .color_f8f8f8
        view.bounces = false
        return view
    }()
    
    lazy var line: UIView = {
        let view = UIView.init()
        view.backgroundColor = .color_e7e7e7
        return view
    }()
    
    lazy var shadowLayerView: UIView = {
        let view = UIView.init()
        view.backgroundColor = UIColor.black.withAlphaComponent(0.4)
        return view
    }()
    
    /// 0表示二级筛选  1表示赛事的三级筛选
    fileprivate var type: Int = 0
    fileprivate var leftIndex: Int = 0
    fileprivate var cityIndex: Int = 0
    
    var itemArray: [BHHomeCityModel] = [] {
        didSet {
            self.provinceTab.reloadData()
        }
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.isHidden = true
        self.addSubview(self.shadowLayerView)
        self.addSubview(self.line)
        self.addSubview(self.locationSecView)
        self.addSubview(self.provinceTab)
        self.addSubview(self.cityTab)
        self.addSubview(self.areaTab)
        
        
        self.shadowLayerView.mas_makeConstraints { (make) in
            make?.left.right()?.bottom()?.top()?.equalTo()(self)
        }
        
        
        self.line.mas_makeConstraints { (make) in
            make?.left.top()?.right().equalTo()(self)
            make?.height.mas_equalTo()(0.5)
        }
        
        self.shadowLayerView.addViewAction {[weak self] (_) in
            self?.showOrHide(show: false)
            self?.delegate?.didPlaceChoseClose()
        }

        self.locationSecView.mas_makeConstraints { (make) in
            make?.left.right()?.equalTo()(self)
            make?.top.mas_equalTo()(0.5)
            make?.height.mas_equalTo()(40)
        }
        
        self.provinceTab.mas_makeConstraints { (make) in
            make?.left.equalTo()(self)
            make?.height.mas_equalTo()(200)
            make?.top.equalTo()(self.locationSecView.mas_bottom)
            make?.width.mas_equalTo()(90)
        }
        
        self.cityTab.mas_makeConstraints { (make) in
            make?.left.mas_equalTo()(90)
            make?.top.equalTo()(self.locationSecView.mas_bottom)
            make?.width.mas_equalTo()(100)
            make?.height.mas_equalTo()(200)
        }
        
        self.areaTab.mas_makeConstraints { (make) in
            make?.left.mas_equalTo()(190)
            make?.right.equalTo()(self)
            make?.top.equalTo()(self.locationSecView.mas_bottom)
            make?.height.mas_equalTo()(200)
        }
    }
}
// animation
extension BHCompetitionAreaChoseView {
    func showOrHide(show: Bool) {
        self.isHidden = !show
    }
   
}


extension BHCompetitionAreaChoseView: UITableViewDataSource {
   
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if tableView == self.provinceTab {
            return self.itemArray.count
        }else if tableView == self.cityTab {
            if self.itemArray.count == 0 {
                return 0
            }
            let model = self.itemArray[self.leftIndex]
            return model.childArray.count
        }else {
            if self.itemArray.count == 0 {
                return 0
            }
            let model = self.itemArray[self.leftIndex]
            if model.childArray.count == 0 {
                return 0
            }
            let areaModel = model.childArray[self.cityIndex]
            return areaModel.childArray.count
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "BHHomeChoseCityCell", for: indexPath) as! BHHomeChoseCityCell
        if tableView == provinceTab {
            let model = self.itemArray[indexPath.row]
            cell.text_label.text = model.name
            cell.canChose = true
            cell.chose = indexPath.row == self.leftIndex
        }else {
            if tableView == self.cityTab {
                let leftModel = self.itemArray[self.leftIndex]
                let model = leftModel.childArray[indexPath.row]
                cell.text_label.text = model.name
                cell.chose = indexPath.row == self.cityIndex
                cell.canChose = true
            }else {
                let leftModel = self.itemArray[self.leftIndex]
                let model = leftModel.childArray[self.cityIndex]
                let areaModel = model.childArray[indexPath.row]
                cell.text_label.text = areaModel.name
                cell.canChose = false
            }
        }
        return cell
    }
}

extension BHCompetitionAreaChoseView: UITableViewDelegate {
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 40
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if tableView == self.provinceTab {
            let model = self.itemArray[indexPath.row]
            self.delegate?.didChoseProvince(model: model)
            self.leftIndex = indexPath.row
            self.cityIndex = 0
            self.provinceTab.reloadData()
            self.cityTab.reloadData()
            self.areaTab.reloadData()
        }else if tableView == self.cityTab {
            let provinceModel = self.itemArray[self.leftIndex]
            let cityModel = provinceModel.childArray[indexPath.row]
            self.delegate?.didChoseCity(model: cityModel)

            self.cityIndex = indexPath.row
            self.cityTab.reloadData()
            self.areaTab.reloadData()
        }
        
        else {
            
            let provinceModel = self.itemArray[self.leftIndex]
            let cityModel = provinceModel.childArray[self.cityIndex]
            let areaModel = cityModel.childArray[indexPath.row]
            self.delegate?.didChoseArea(areaModel: areaModel)
            
            self.showOrHide(show: false)
            self.delegate?.didPlaceChoseClose()
        }
    }
}
